<?php
namespace app\www\controller;

class Index extends Base
{

    function init()
    {
        #幻灯
        $slides = $this->db->cache("select id,url,pic,cat_id,background_color from slide where cat_id in (6,7,8,9,10,11,12) order by sort desc,id desc");
        $slide_temp = [];
        foreach ($slides as $v) {
            if (!isset($slide_temp[$v['cat_id']])) $slide_temp[$v['cat_id']] = [];
            $slide_temp[$v['cat_id']][] = $v;
        }
        #一级分类标题
        $goods_cat_ids = [
            $this->sys['indexf_cat_1'],
            $this->sys['indexf_cat_2'],
            $this->sys['indexf_cat_3'],
            $this->sys['indexf_cat_4'],
            $this->sys['indexf_cat_5'],
            $this->sys['indexf_cat_6']
        ];
        $one_cats = $this->db->cache("select id,cat_title from goods_cat where id in (" . implode(',', $goods_cat_ids) . ") and status=0 order by sort desc");
        $one_cat_temp = [];
        foreach ($one_cats as $v) {
            if (!isset($one_cat_temp[$v['id']])) $one_cat_temp[$v['id']] = [];
            $one_cat_temp[$v['id']][] = $v;
        }

        #二级分类
        $two_cats = $this->db->cache("select id,cat_title,parent_id from goods_cat where parent_id in (" . implode(',', $goods_cat_ids) . ") and status=0 order by sort desc limit 6");
        $two_cat_temp = [];
        foreach ($two_cats as $v) {
            if (!isset($two_cat_temp[$v['parent_id']])) $two_cat_temp[$v['parent_id']] = [];
            $two_cat_temp[$v['parent_id']][] = $v;
        }
        #产品
        $sql = "";
        $cat_goods = [];
        foreach ($goods_cat_ids as $k => $cid) {
//            $ids = m('GoodsCat','c')->getAllchildCat($cid);
//            $ids[] = $cid;
//            $str = implode(',',$ids);
//            $sql = "select * from goods where cat_id in (".$str.") order by sort desc,id desc";
//            $rs = $this->db->query($sql);
            $rec_id = "," . ($k + 1) . ",";
            $rs = $this->db->query("select * from goods where rec_id like '%{$rec_id}%' and status = 0 order by sort desc,id desc limit 12", []);
            $cat_goods[$cid] = $rs;
        }


        //公告
        $notice = $this->db->cache('select a.id,a.title,a.content,b.cat_title from article a left join article_cat b on a.cat_id=b.id where a.cat_id = ? order by a.sort desc,a.id desc', [8]);
        $indexf = [
            'notice' => $notice,
            'slide' => $slide_temp,
            'two_cat' => $two_cat_temp,
            'one_cat' => $one_cat_temp,
            'goods' => $cat_goods,
        ];
        $this->fetch(['indexf' => $indexf]);
    }




    public function excute()
    {

        $this->getpic();
    }

    public function get_article()
    {

    }



    function test()
    {
        //m('goods','c')->updateStock(1,309);
        #1,产品分类=> 1,清我们的产品分类和， 2，导入分类，3，导入产品，4，产品图片附件，5，下载远程图片
        // $rs = $this->db1->query('select * from goodstypethree');
        // foreach ($rs as $k => $v) {
        //     $re1 = $this->db1->query('select * from goodstypetwo where id = ?',[$v['typeTwoId']],1);
        //     $re2 = $this->db->query('select * from goods_cat where id = ?',[$re1['old_id']],1);
        //     $data = [
        //         'parent_id'=> $re1['old_id'],
        //         'cat_title'=> $v['name'],
        //         'sub_title'=> $v['name'],
        //         'cat_tree'=> $re2['cat_tree'] . $re2['id'] . ',',
        //         'sort'=> $v['list'],
        //         'level'=> 3,
        //         'create_time'=> time(),
        //     ];
        //     $id = $this->db->insert('goods_cat',$data);
        //
        // }
        // $rs = $this->db->query('select * from goods_cat');
        // foreach($rs as $v){
        //     $d = [
        //         'id' => $v['id'],
        //         'level' => count(explode(',',$v['cat_tree']))-3,
        //     ];
        //     $this->db->update('goods_cat',$d);
        // }
        // e(date('Ymd H:i:s'));
        //js_url('',0.01);
    }

}

?>
